package com.qxmly.fm.presenters;

import static com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl.PlayMode.PLAY_MODEL_LIST;

import com.qxmly.fm.base.BaseApplication;
import com.qxmly.fm.interfaces.IPlayerCallBack;
import com.qxmly.fm.interfaces.IPlayerPresenter;
import com.qxmly.fm.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.model.PlayableModel;
import com.ximalaya.ting.android.opensdk.model.advertis.Advertis;
import com.ximalaya.ting.android.opensdk.model.advertis.AdvertisList;
import com.ximalaya.ting.android.opensdk.model.track.Track;
import com.ximalaya.ting.android.opensdk.player.XmPlayerManager;
import com.ximalaya.ting.android.opensdk.player.advertis.IXmAdsStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.IXmPlayerStatusListener;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayListControl;
import com.ximalaya.ting.android.opensdk.player.service.XmPlayerException;

import java.util.ArrayList;
import java.util.List;

/**
 * Q
 * Date:2021/11/30$
 **/
public class PlayerPresenter implements IPlayerPresenter, IXmAdsStatusListener, IXmPlayerStatusListener {

    private static final String TAG = "PlayerPresenter";

    private List<IPlayerCallBack> mIplayerCallBack = new ArrayList<>();
    private Track mCurrentTrack;
    private XmPlayerManager mplayerManager;
    public static final int DEFAULT_PLAY_INDEX = 0;
    private int mCurrentIndex = DEFAULT_PLAY_INDEX;
    private XmPlayListControl.PlayMode mCurrentPlayMode = PLAY_MODEL_LIST;
    private boolean mIsReverse = false;

    private int mCurrentProgressPosition = 0;
    private int mProgressDuration = 0;
    private String mtrackTitle;

    private PlayerPresenter() {
        //获取播放实例
      mplayerManager = XmPlayerManager.getInstance(BaseApplication.getAppContext());
      //广告物料接口
      mplayerManager.addAdsStatusListener(this);
      //注册播放器状态接口
        mplayerManager.addPlayerStatusListener(this);
    }

    private static PlayerPresenter sPlayerPresenter;

    public static PlayerPresenter getPlayerPresenter() {
        if (sPlayerPresenter == null) {
            synchronized (PlayerPresenter.class) {
                if (sPlayerPresenter == null) {
                    sPlayerPresenter = new PlayerPresenter();
                }
            }
        }
        return sPlayerPresenter;
    }

    //是否设置播放器列表
    private boolean isPlayListSet = false;

    /**
     * 设置播放列表
     *
     * @param list
     * @param playIndex
     */
    public void setPlayList(List<Track> list, int playIndex) {
        if (mplayerManager != null) {
            mplayerManager.setPlayList(list, playIndex);
            isPlayListSet = true;
            mCurrentTrack = list.get(playIndex);
            mCurrentIndex = playIndex;
            Track track  = list.get(playIndex);
            mtrackTitle = track.getTrackTitle();
        } else {
            LogUtil.d(TAG, "mPlayerManager is null！");
        }
    }


    @Override
    public void registerCallBack(IPlayerCallBack iPlayerCallBack) {
        iPlayerCallBack.onTrackTitle(mtrackTitle);
        if(!mIplayerCallBack.contains(iPlayerCallBack)){
            mIplayerCallBack.add(iPlayerCallBack);
        }

    }

    @Override
    public void UnregisterCallBack(IPlayerCallBack iPlayerCallBack) {
            mIplayerCallBack.remove(iPlayerCallBack);
    }

    @Override
    public void play() {
        if (isPlayListSet) {
            mplayerManager.play();
        }
    }

    @Override
    public void pause() {
        if (isPlayListSet) {
            mplayerManager.pause();
        }
    }

    @Override
    public void stop() {
        if (isPlayListSet) {
            mplayerManager.stop();
        }
    }

    @Override
    public void playPre() {
        //播放前一个节目
        if (mplayerManager != null) {
            mplayerManager.playPre();
        }
    }

    @Override
    public void playNext() {
        //播放下一个节目
        if (mplayerManager != null) {
            mplayerManager.playNext();
        }
    }

    @Override
    public void switchPlayMode(XmPlayListControl.PlayMode mode) {

    }

    @Override
    public void getPlayList() {

    }

    @Override
    public void playByIndex(int index) {

    }

    @Override
    public void seekTo(int progress) {
        //更新播放器进度
        mplayerManager.seekTo(progress);


    }

    @Override
    public boolean isPlaying() {
      return  mplayerManager.isPlaying();
    }

    @Override
    public void reversePlayList() {

    }

    @Override
    public void playByAlbumId(long id) {

    }
    //
    @Override
    public void onStartGetAdsInfo() {
        LogUtil.d(TAG, "onStartGetAdsInfo...");
    }

    @Override
    public void onGetAdsInfo(AdvertisList advertisList) {
        LogUtil.d(TAG, "onGetAdsInfo...");
    }

    @Override
    public void onAdsStartBuffering() {
        LogUtil.d(TAG, "onAdsStartBuffering...");
    }

    @Override
    public void onAdsStopBuffering() {
        LogUtil.d(TAG, "onAdsStopBuffering...");
    }

    @Override
    public void onStartPlayAds(Advertis advertis, int i) {
        LogUtil.d(TAG, "onStartPlayAds...");
    }

    @Override
    public void onCompletePlayAds() {
        LogUtil.d(TAG, "onStartPlayAds...");
    }

    @Override
    public void onError(int i, int i1) {
        LogUtil.d(TAG, "what ----> " + i + "extra ---->" + i1);
    }

    //
    @Override
    public void onPlayStart() {
        for(IPlayerCallBack iPlayerCallBack : mIplayerCallBack){
            iPlayerCallBack.onPlayStart();
        }

    }

    @Override
    public void onPlayPause() {
        for(IPlayerCallBack iPlayerCallBack : mIplayerCallBack){
            iPlayerCallBack.onPlayPause();
        }
    }

    @Override
    public void onPlayStop() {
        for(IPlayerCallBack iPlayerCallBack : mIplayerCallBack){
            iPlayerCallBack.onPlayStop();
        }
    }

    @Override
    public void onSoundPlayComplete() {

    }

    @Override
    public void onSoundPrepared() {

    }

    @Override
    public void onSoundSwitch(PlayableModel playableModel, PlayableModel playableModel1) {
        //上下首切换
//        if ("track".equals(playableModel1.getKind())){
//            Track currentTrack = (Track) playableModel1;
//
//        }
        if(playableModel1 instanceof Track) {
            Track currentTrack = (Track) playableModel1;
            mtrackTitle = currentTrack.getTrackTitle();

            for (IPlayerCallBack iPlayerCallBack : mIplayerCallBack) {
                iPlayerCallBack.onTrackTitle(mtrackTitle);
            }
        }
    }

    @Override
    public void onBufferingStart() {

    }

    @Override
    public void onBufferingStop() {

    }

    @Override
    public void onBufferProgress(int i) {

    }

    @Override
    public void onPlayProgress(int i, int i1) {

        for (IPlayerCallBack iPlayerCallBack:mIplayerCallBack){
            iPlayerCallBack.onProgressChange(i,i1);
        }

    }

    @Override
    public boolean onError(XmPlayerException e) {
        return false;
    }
}
